#include "vconnection.h"
#include "ui_vconnection.h"
#include <QNetworkInterface>
#include "qdebug.h"

VConnection::VConnection(QWidget *parent)
    : QFrame(parent)
    , ui(new Ui::VConnection)
{
    ui->setupUi(this);
    this->refreshIPList();

    m_strListBtnText<<tr("监听")<<tr("监听中")<<tr("关闭");

    connect(ui->comboBox_ip,&QComboBox::currentTextChanged,this,&VConnection::slotArgReset);
    connect(ui->lineEdit_port,&QLineEdit::textChanged,this,&VConnection::slotArgReset);
}

VConnection::~VConnection()
{
    delete ui;
}


/****************************************************
 * 刷新ip地址、端口
 * 点击连接按钮
*****************************************************/
void VConnection::refreshIPList()
{
    // 清除旧的IP地址列表
    ui->comboBox_ip->clear();

    // 获取所有网络接口
    QList<QNetworkInterface> interfaces = QNetworkInterface::allInterfaces();

    // 遍历每个网络接口
    foreach (QNetworkInterface interface, interfaces) {
        // 排除回环接口和虚拟接口
        if (interface.flags().testFlag(QNetworkInterface::IsLoopBack))
            continue;

        // 获取该接口上的所有地址
        QList<QNetworkAddressEntry> addresses = interface.addressEntries();
        foreach (QNetworkAddressEntry address, addresses) {
            // 判断地址是否为IPv4地址
            if (address.ip().protocol() == QAbstractSocket::IPv4Protocol) {
                ui->comboBox_ip->addItem(address.ip().toString());
            }
        }
    }
}

void VConnection::setConnectionProxy(CUdp *pConnectManager)
{
    m_pConnect = pConnectManager;
    connect(m_pConnect,&CUdp::sigStateChanged,[=](CUdp::EnmState enmState){
        this->stateChanged(enmState);
    });
    connect(m_pConnect,&CUdp::sigConnectChanged,[=](QString strIp,quint16 nPort,bool bState){
        this->connectStateChanged(strIp,nPort,bState);
    });
    connect(m_pConnect,&CUdp::sigDataArrive,[=](QString strIp,quint16 nPort,QByteArray byteArr){
        this->dataArrive(strIp,nPort,byteArr);
    });

}

void VConnection::on_pushButton_connect_clicked(bool checked)
{
    if (checked)
    {
        // 连接按钮被选中
        StSocketInfo m_pSocketInfo(ui->comboBox_ip->currentText(),ui->lineEdit_port->text().toUShort());
        // qDebug() << "Connecting to" << m_pSocketInfo.strIp << ":" << m_pSocketInfo.nPort;

        // 创建 CUdp 对象并开始连接
        connect(m_pConnect,&CUdp::sigStateChanged,[=](CUdp::EnmState enmState){
            this->stateChanged(enmState);
        });
        m_pConnect->startConnection(&m_pSocketInfo);
    }
    else
    {
        // 连接按钮被取消选中
        // qDebug() << "Disconnecting";
        m_pConnect->stopConnection();
        this->refreshIPList();
    }
}

void VConnection::stateChanged(CUdp::EnmState enmState)
{
    static QStringList strListColor = {"#ff4d4d","#ffaa00","#47d147"}; //红、黄、绿
    int nIndex = (int)enmState;
    // qDebug() << "nIndex:"<<nIndex;
    if(strListColor.size() > nIndex && m_strListBtnText.size() > nIndex)
    {
        ui->label_state->setStyleSheet(QString("border-radius:5px;background:%1;").arg(strListColor[nIndex]));//状态点状态变化
        ui->pushButton_connect->setText(m_strListBtnText[nIndex]); //按钮文本变化

        //断开连接时
        if(CUdp::ENM_NOT_READY == enmState){
            ui->pushButton_connect->setChecked(false); //连接按钮置回未连接状态
        }
    }
}

void VConnection::slotArgReset()
{
    //已打开连接时将button，修改链接状态
    if(ui->pushButton_connect->isChecked())
    {
        ui->pushButton_connect->click(); //停止
        ui->pushButton_connect->click(); //开始
    }
}
